<?php
namespace Home\Controller;
use Home\Controller\BaseController;

class FriendlinkController extends BaseController{
	private $M;
	public function _initialize()
	{
		$this->M = M("friend_link");
	}
	/*
    *   分页显示
    */
    public function index(){
    	$FriendlinkInfo = M('friend_link')->limit(0,10)->select();
		$this->assign('FriendlinkInfo',$FriendlinkInfo);

        $this->display();
    }

	 public function edit(){
        $id = I("id");
        if(!empty($id)){
            $data = $this->M->where("id=".$id)->select();
            $data = $data[0];
            $this->assign("data", $data);
        }
        $this->display();
    }
    /*
    *   添加/修改友情链接
    */

	 public function doEdit($sitename, $brief, $website){
        // 图片上传
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =     CONTROLLER_NAME.'/'; // 设置附件上传目录    // 上传文件
        $upload->subName = array('date','Y/m');
        $logoname = I("logoname");
        if(!empty($logoname)){
            unlink(C("TMPL_PARSE_STRING")["__UPLOAD__"]."/".CONTROLLER_NAME."/".$logoname);
        }
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            $logo = substr($info["logo"]["savepath"],11) . $info["logo"]["savename"];
        }
        $data = $this->M->create();
        if(!empty($logo)){
            $data["logo"] = $logo;
        }
        if(empty($data["id"])){
            $res = $this->M->data($data)->add();
        }
        else{
            $res = $this->M->data($data)->save();
        }
        if($res){
            $this->success("操作成功", U("Friendlink/index"));
        }
        else{
            $this->error("未修改数据", U("Friendlink/edit"));
        }
    }

    public function del(){
        $id = I("id");
        $data = $this->M->where("id=".$id)->select();
        $conf = C("TMPL_PARSE_STRING");
        $path = $conf["__UPLOAD__"];
        $rmimg = unlink($path."/".$data[0]["logo"]);
        if($rmimg){
            $res = $this->M->where("id=".$id)->delete();
        }
        if($res){
            $this->success("操作成功", U("Friendlink/index"));
        }
        else{
            $this->error("操作失败", U("Friendlink/index"));
        }
    }

}